package com.wcy.entity.queryVo;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

//const operatorSelection = {
//text:[{label: '等于', value: '='},{label: '包含', value: 'like'}, {label: '不包含', value: 'not like'}],
//textarea:[{label: '等于', value: '='},{label: '包含', value: 'like'}, {label: '不包含', value: 'not like'}],
//password:[{label: '等于', value: '='},{label: '包含', value: 'like'}, {label: '不包含', value: 'not like'}],
//number:[{label: '等于', value: '='}, {label: '大于', value: '>'}, {label: '小于', value: '<'}, {label: '大于等于', value: '>='}, {label: '小于等于', value: '<='},  {label: '不等于', value: '!='}],
//        'date-picker':[{label: '晚于', value: '>'}, {label: '早于', value: '<'}],
//select:[{label: '属于', value: '='}, {label: '不属于', value: '!='}],
//        }
public class QueryConvertor<T> {
    private RawQueryCondition[] result;
    private boolean disableOr=false;
    public QueryConvertor(String jsonData) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            result = mapper.readValue(URLDecoder.decode(jsonData, StandardCharsets.UTF_8), RawQueryCondition[].class);
        }catch(Exception e){
            System.out.println("无查询内容");
        }
    }

    public QueryConvertor(String jsonData,Boolean disableOr) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            result = mapper.readValue(URLDecoder.decode(jsonData, StandardCharsets.UTF_8), RawQueryCondition[].class);
        }catch(Exception e){
            System.out.println("无查询内容");
        }
    }

    public QueryWrapper<T> convert(boolean convertNaming){
        if(result == null){
            return new QueryWrapper<>();
        }
        return convert(List.of(result),  convertNaming);
    }

    private QueryWrapper<T> convert(List<RawQueryCondition> rawQueryDataList,boolean convertNaming) {
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        for (RawQueryCondition rawQueryData : rawQueryDataList) {
            String field = rawQueryData.getField();
            if (convertNaming) {
                field = field.replaceAll("([a-z])([A-Z])", "$1_$2").toLowerCase();
            }
            if(rawQueryData.getMethod()==null||rawQueryData.getMethod().isEmpty())
                continue;
            if (!disableOr&&"OR".equals(rawQueryData.getOperator())) {
                queryWrapper.or();
            }
            switch (rawQueryData.getType()) {
                case "listQuery":{
                    queryWrapper.in(field, List.of(rawQueryData.getValue().split(",")));
                    //{
                    //    operator: "AND",
                    //    field: 'id',
                    //    fieldName: 'x',
                    //    type: 'listQuery',
                    //    method: "<any>",
                    //    value: 'id1,id2,id3,id4',
                    //  }
                }break;
                case "text":
                case "textarea":
                case "password":
                    switch (rawQueryData.getMethod()) {
                        case "=":
                            queryWrapper.eq(field, rawQueryData.getValue());
                            break;
                        case "like":
                            queryWrapper.like(field, rawQueryData.getValue());
                            break;
                        case "not like":
                            queryWrapper.notLike(field, rawQueryData.getValue());
                            break;
                    }
                    break;
                case "number":
                    switch (rawQueryData.getMethod()) {
                        case "=":
                            queryWrapper.eq(field, rawQueryData.getValue());
                            break;
                        case ">":
                            queryWrapper.gt(field, rawQueryData.getValue());
                            break;
                        case "<":
                            queryWrapper.lt(field, rawQueryData.getValue());
                            break;
                        case ">=":
                           queryWrapper.ge(field, rawQueryData.getValue());
                    }
                    break;
                case "date-picker":
                    switch (rawQueryData.getMethod()) {
                        case ">":
                            queryWrapper.gt(field, rawQueryData.getValue());
                            break;
                        case "<":
                            queryWrapper.lt(field, rawQueryData.getValue());
                            break;
                    }
                    break;
                case "select":
                    switch (rawQueryData.getMethod()) {
                        case "=":
                            queryWrapper.eq(field, rawQueryData.getValue());
                            break;
                        case "!=":
                            queryWrapper.ne(field, rawQueryData.getValue());
                            break;
                    }
                    break;
            }
        }
        return queryWrapper;
    }

}
